package com.itheima.controller;

import com.itheima.domain.Student;
import com.itheima.service.StudentService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller("studentController")
@RequestMapping(value = "/student",produces = "application/json; charset=utf-8")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("/list.jsp");
        mv.addObject("list",studentService.findAll());
        return mv;
    }

    @RequestMapping("/update")
    public String update(Student student){
        studentService.update(student);
        return "redirect:/student/findAll";
    }

    @RequestMapping("/findById")
    public ModelAndView findById(Integer id){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("/edit.jsp");
        mv.addObject("student",studentService.findById(id));
        return mv;
    }

    @RequestMapping("/save")
    public String save(Student student){
        studentService.save(student);
        return "redirect:/student/findAll";
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        studentService.delete(id);
        return "redirect:/student/findAll";
    }


}
